package cn.imnaer.common.factory;

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;


public class JacksonUtil {

    
    private static ObjectMappingCustomer mapper;

    /**
     * 获取ObjectMappingCustomer实例
     *
     * @param createNew 方式：true，新实例；false,存在的mapper实例
     * @return
     */
    public static synchronized ObjectMappingCustomer getMapperInstance(boolean createNew) {
        if (createNew) {
            return new ObjectMappingCustomer();
        } else if (mapper == null) {
            mapper = new ObjectMappingCustomer();
        }
        return mapper;
    }

    /**
     * 将java对象转换成json字符串
     *
     * @param obj 准备转换的对象
     * @return json字符串
     * @throws Exception
     */
    public static String beanToJson(Object obj) {
        String json = null;
        try {
            ObjectMappingCustomer ObjectMappingCustomer = getMapperInstance(false);
            json = ObjectMappingCustomer.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return json;
    }

    /**
     * 将java对象转换成json字符串
     *
     * @param obj       准备转换的对象
     * @param createNew ObjectMappingCustomer实例方式:true，新实例;false,存在的mapper实例
     * @return json字符串
     * @throws Exception
     */
    public static String beanToJson(Object obj, Boolean createNew) {
        String json = null;
        try {
            ObjectMappingCustomer ObjectMappingCustomer = getMapperInstance(createNew);
            json = ObjectMappingCustomer.writeValueAsString(obj);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 将json字符串转换成java对象
     *
     * @param json 准备转换的json字符串
     * @param cls  准备转换的类
     * @return
     * @throws Exception
     */
    public static <T> T jsonToBean(String json, Class<T> cls) {
        Object vo = null;
        try {
            ObjectMappingCustomer ObjectMappingCustomer = getMapperInstance(false);
            vo = ObjectMappingCustomer.readValue(json, cls);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return (T)vo;
    }

    /**
     * 将json字符串转换成java对象
     *
     * @param json      准备转换的json字符串
     * @param cls       准备转换的类
     * @param createNew ObjectMappingCustomer实例方式:true，新实例;false,存在的mapper实例
     * @return
     * @throws Exception
     */
    public static Object jsonToBean(String json, Class<?> cls, Boolean createNew) {
        Object vo = null;
        try {
            ObjectMappingCustomer ObjectMappingCustomer = getMapperInstance(createNew);
            vo = ObjectMappingCustomer.readValue(json, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }
    
   public   static <T> List<T>  jsonToList(String  jsonData,T data){
    ObjectMappingCustomer mapper = JacksonUtil.getMapperInstance(false);
    List<T> list=null;
        try {
			list = mapper.readValue(jsonData,
			        new TypeReference<List<T>>() {
			        });
		} catch(Exception e) {		
			e.printStackTrace();
			return  null;
		}
       
	return  list;
}
}
